package surf.browser.component;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

import org.mozilla.browser.MozillaConfig;

import surf.browser.Settings;

/**
 * Proxy Settings dialog.
 * @author Tom Cashman
 *
 */
public class ProxyWindow extends JFrame {
	private static final long serialVersionUID = 1L;
	private int m_selected;
	private JButton noProxy, manualProxy, autoProxy;
	private JTextField httpHost, httpPort, sslHost, sslPort, ftpHost, ftpPort, socksHost, socksPort, noProxyFor, automatic;

	/**
	 * Default constructor. Sets up the entire dialog.
	 */
	public ProxyWindow() {
		super("Proxy Settings");
		m_selected = 0;
		this.setSize(280, 320);
		this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.PAGE_AXIS));
		noProxy = new JButton("No Proxy");
		noProxy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				manualProxy.setEnabled(true);
				autoProxy.setEnabled(true);
				noProxy.setEnabled(false);
				m_selected = 0;
			}
		});
		noProxy.setEnabled(false);
		this.getContentPane().add(noProxy);
		
		manualProxy = new JButton("Manual Proxy");
		manualProxy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				noProxy.setEnabled(true);
				autoProxy.setEnabled(true);
				manualProxy.setEnabled(false);
				m_selected = 1;
			}
		});
		manualProxy.setVisible(true);
		this.getContentPane().add(manualProxy);
		
		autoProxy = new JButton("Automatic Proxy");
		autoProxy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				noProxy.setEnabled(true);
				autoProxy.setEnabled(false);
				manualProxy.setEnabled(true);
				m_selected = 1;
			}
		});
		autoProxy.setVisible(true);
		this.getContentPane().add(autoProxy);
		
		JButton save = new JButton("Save");
		save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				switch(m_selected) {
				case 0:
					MozillaConfig.setAutomaticProxy("http://proxy.dcu.ie/proxy.pac");
					break;
				case 1:
					MozillaConfig.setManualProxy("proxy.dcu.ie", 8080,"proxy.dcu.ie", 8080,"proxy.dcu.ie", 3128, "proxy.dcu.ie", 1080,"");
					break;
				case 2:
					break;
				}
				Settings.getProxyWindow().dispose();
			}
		});
		this.getContentPane().add(save);
		
		JButton close = new JButton("Close");
		close.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Settings.getProxyWindow().dispose();
			}
		});
		this.getContentPane().add(close);
		this.setVisible(true);
	}
}
